const db = wx.cloud.database()
const commoditiesCollection = db.collection('commodities')
const favoriteCollection = db.collection('favorite')
// pages/user/favorite/favorite.js
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    db.collection('favorite').get().then(res => {
      console.log(res.data)
      this.setData({
        favorite: res.data
      })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  navigateToCommodities: function (e) {
    var goodsID = e.currentTarget.dataset.gid;
    var typeID = e.currentTarget.dataset.tid;
    var id = e.currentTarget.dataset.id;
    // console.log("navigateToCommodities-->goodsID:", goodsID)
    // console.log("navigateToCommodities-->typeID:", typeID)
    // console.log("navigateToCommodities-->", id)
    console.log("即将跳转到第", typeID, "类的第", goodsID, "个商品详情")
    wx.navigateTo({
      url: '../../first/commodities/commodities?typeID=' + typeID + '&goodsID=' + goodsID + '&id=' + id,

    })
  },

  del(e) {
    const index = e.currentTarget.dataset.index;
    const id = e.currentTarget.dataset.id;
    let favorite = this.data.favorite;
    favorite.splice(index, 1);
    this.setData({
      favorite: favorite
    });
    favoriteCollection.doc(id).remove({
      success: console.log,
      fail: console.error
    })
  }

})